/**
 * Candidate number: 35834
 */
package tv.starfu.animae;

import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.World;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.MeshCollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.ChaseCamera;
import com.jme3.input.FlyByCamera;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Sphere;
import com.jme3.texture.Texture;
import com.jme3.util.TangentBinormalGenerator;
import tv.starfu.components.Transform;

/**
 * Anima /Player/ is owned by Entity [ArcadePlayer]
 * @author niadzi
 */
public class Player extends Anima {
    
    private Transform transform;
    private Geometry vehicle;
    private Node playerNode = new Node("playerNode");
    private Node playerGuiNode = new Node("playerGuiNode");
    private BulletAppState bullet;
    private CharacterControl playerCtrl;
    
    private SimpleApplication app;
    private Camera cam;
    private ChaseCamera chaseCam;
    
    // + anything else needed to render player, eg OpenGL, pics etc
    
    
    public Player(World world, Entity owner, SimpleApplication app, Camera sceneCam, Node parentNode, BulletAppState bullet) {
        super(world, owner, app);
        this.app = app;
        this.cam = sceneCam;
        this.bullet = app.getStateManager().getState(BulletAppState.class);
        playerNode = parentNode;
        FlyByCamera flyCam = app.getFlyByCamera();
        flyCam.setEnabled(false);
        
    }
    
    @Override
    public void initalize() {
        ComponentMapper<Transform> transformMapper = new ComponentMapper<Transform>(Transform.class, world);
        transform = transformMapper.get(owner);
        AssetManager assetManager = app.getAssetManager();
        
        Sphere sphere = new Sphere(32,32,10f);
        vehicle = new Geometry("Sphere", sphere);
        
        sphere.setTextureMode(Sphere.TextureMode.Projected);
        TangentBinormalGenerator.generate(sphere);
        
        Material material = new Material(assetManager, 
                "Common/MatDefs/Misc/Unshaded.j3md");
        Texture discoBalls = assetManager.loadTexture("Textures/terrain/smoky.jpg");
        material.setTexture("ColorMap", discoBalls);
        
        vehicle.setMaterial(material);
        vehicle.lookAt(Vector3f.UNIT_Z, Vector3f.UNIT_Y);
        playerNode.attachChild(vehicle);
        //System.out.println("Made player vehicle...");
        
        SphereCollisionShape playerMesh = new SphereCollisionShape(1.0f);
        playerCtrl = new CharacterControl(playerMesh, 1f);
        playerCtrl.setJumpSpeed(20);
        playerCtrl.setFallSpeed(30);
        playerCtrl.setGravity(30);
        playerCtrl.setPhysicsLocation(new Vector3f(0, 30, 0));
              
        //Enable ChaseCam
        chaseCam = new ChaseCamera(cam, vehicle, app.getInputManager());
        chaseCam.setSmoothMotion(true);
        chaseCam.setDefaultDistance(40f);
        chaseCam.setDragToRotate(true);
        chaseCam.setMaxDistance(60f);
        chaseCam.setMinDistance(30f);
        chaseCam.setDefaultVerticalRotation(FastMath.QUARTER_PI*0.5f);
        chaseCam.setDefaultHorizontalRotation(-FastMath.HALF_PI);
        chaseCam.setTrailingRotationInertia(0.4f);  // not sure how much difference this makes...
        chaseCam.setTrailingEnabled(true);
        
      //  this.get
        bullet.getPhysicsSpace().add(playerCtrl);
    }

    @Override
    public void render() {
        playerCtrl.setPhysicsLocation(transform.getPosition());
        vehicle.setLocalTranslation(playerCtrl.getPhysicsLocation());
        
    }
    
}
